package dao;

import db.DBUtil;
import model.Product;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品数据访问类，使用老板数据库存储商品信息
 */
public class ProductDAO {
    
    /**
     * 根据商品ID查询商品
     */
    public Product getProductById(String productId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Product product = null;
        
        try {
            conn = DBUtil.getConnection(); // 商品信息存储在老板数据库中
            String sql = "SELECT * FROM products WHERE product_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, productId);
            rs = pstmt.executeQuery();
            
            if (rs.next()) {
                product = new Product();
                product.setProductId(rs.getString("product_id"));
                product.setName(rs.getString("name"));
                product.setPrice(rs.getDouble("price"));
                product.setPurchasePrice(rs.getDouble("purchase_price"));
                product.setStock(rs.getInt("stock"));
                product.setProductionDate(rs.getString("production_date"));
                product.setShelfLife(rs.getInt("shelf_life"));
                product.setSupplierId(rs.getString("supplier_id"));
            }
        } catch (SQLException e) {
            System.err.println("查询商品失败: " + e.getMessage());
        } finally {
            try {
                if (rs != null) rs.close();
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        return product;
    }
    
    /**
     * 更新商品库存
     */
    public boolean updateStock(String productId, int quantity) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "UPDATE products SET stock = stock + ? WHERE product_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, quantity);
            pstmt.setString(2, productId);
            
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            System.err.println("更新库存失败: " + e.getMessage());
            return false;
        } finally {
            try {
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    /**
     * 查询库存不足的商品
     */
    public List<Product> getLowStockProducts(int threshold) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Product> products = new ArrayList<>();
        
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM products WHERE stock < ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, threshold);
            rs = pstmt.executeQuery();
            
            while (rs.next()) {
                Product product = new Product();
                product.setProductId(rs.getString("product_id"));
                product.setName(rs.getString("name"));
                product.setPrice(rs.getDouble("price"));
                product.setStock(rs.getInt("stock"));
                product.setSupplierId(rs.getString("supplier_id"));
                products.add(product);
            }
        } catch (SQLException e) {
            System.err.println("查询低库存商品失败: " + e.getMessage());
        } finally {
            try {
                if (rs != null) rs.close();
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        return products;
    }
}
    